module.exports = {
  up: async (queryInterface) => {
    // 查询现有的 plans
    const plans = await queryInterface.sequelize.query(
      'SELECT id, code FROM plans ORDER BY sort_order',
      { type: queryInterface.sequelize.QueryTypes.SELECT }
    );

    // 更新每个 plan
    for (const plan of plans) {
      const updates = {};
      
      switch (plan.code) {
        case 'free':
          updates.max_projects = 3;
          updates.support_level = 'basic';
          updates.has_api_access = false;
          updates.has_custom_domain = false;
          updates.has_white_label = false;
          updates.has_sso = false;
          updates.has_advanced_analytics = false;
          break;
          
        case 'basic':
          updates.max_projects = 10;
          updates.support_level = 'basic';
          updates.has_api_access = false;
          updates.has_custom_domain = false;
          updates.has_white_label = false;
          updates.has_sso = false;
          updates.has_advanced_analytics = false;
          break;
          
        case 'professional':
        case 'pro':
          updates.max_projects = 50;
          updates.support_level = 'priority';
          updates.has_api_access = true;
          updates.has_custom_domain = true;
          updates.has_white_label = false;
          updates.has_sso = false;
          updates.has_advanced_analytics = true;
          break;
          
        case 'enterprise':
          updates.max_projects = null;
          updates.support_level = 'dedicated';
          updates.has_api_access = true;
          updates.has_custom_domain = true;
          updates.has_white_label = true;
          updates.has_sso = true;
          updates.has_advanced_analytics = true;
          break;
          
        default:
          // 其他 plan 使用默认值
          updates.max_projects = 10;
          updates.support_level = 'basic';
          updates.has_api_access = false;
          updates.has_custom_domain = false;
          updates.has_white_label = false;
          updates.has_sso = false;
          updates.has_advanced_analytics = false;
      }
      
      // 执行更新
      await queryInterface.sequelize.query(
        `UPDATE plans 
         SET max_projects = ?, support_level = ?, has_api_access = ?, 
             has_custom_domain = ?, has_white_label = ?, has_sso = ?, 
             has_advanced_analytics = ?
         WHERE id = ?`,
        {
          replacements: [
            updates.max_projects,
            updates.support_level,
            updates.has_api_access,
            updates.has_custom_domain,
            updates.has_white_label,
            updates.has_sso,
            updates.has_advanced_analytics,
            plan.id,
          ],
        }
      );
    }
    
    console.log('✓ Updated existing plans data successfully');
  },

  down: async (queryInterface) => {
    // 不需要回滚，因为这些字段都有默认值
    console.log('Rollback: Skipping (fields have default values)');
  },
};

